package com.aaa.service.impl;

import com.aaa.entity.MyPage;
import com.aaa.entity.Role;
import com.aaa.mapper.RoleMapper;
import com.aaa.service.RoleService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleMapper roleMapper;

    /**
     * 查询角色的全部名称业务
     * @return
     */
    @Override
    public List<Role> findRoleName() {
        List<Role> roles = roleMapper.selectAll();
        return roles;
    }
    /**
     * 查询角色信息
     * @return
     */
    @Override
    public PageInfo selectAll(MyPage myPage) {
        PageHelper.startPage(myPage.getPage(),myPage.getLimit());
        List<Role> userList = roleMapper.selectAll();
        PageInfo pageInfo = new PageInfo(userList);
        return pageInfo;
    }
    /***
     * 添加角色
     */
    @Override
    public Boolean addRole(Role role) {
        int i=-1;
        Integer id = role.getId();
        if(id==null){
            role.setCreatetime(new Date());
            i = roleMapper.insertSelective(role);
        }else{
            i =roleMapper.updateByPrimaryKeySelective(role);

        }
        return i>0?true:false;
    }

    /**
     * 检测用户名是否存在业务
     * @param
     * @return
     */
    @Override
    public Boolean checkName(String rolename) {
        Role role = new Role();
        role.setRolename(rolename);
        Role role1 = roleMapper.selectOne(role);
        if(role1!=null){
            System.out.println("存在");
            return true;
        }else {
            return false;
        }
    }
    /**
     * 删除
     * @param
     */
    @Override
    public Boolean delRole(Role role){
        int count = roleMapper.selectRole(role.getId());
        if(count==0){
            int delete = roleMapper.delete(role);
            return true;
        }
        else {
            return false;
        }
    }
}
